 package baiduClub.schedule;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;




public class XmlConductor {
	
	public int[] fromWeekInt = new int[6];
	public int[] toWeekInt = new int[6];
	public int dayOfWeekInt;
	public int[] id = new int[6];
	
	public static Document addFileDocument;
	public static Document mainFileDocument;
	
	public String[] courseNameString = new String[6];
	public String[] placeString = new String[6];
	
	public int[] getFromWeekInt()
	{
		return fromWeekInt;
	}
	
	public int[] getToWeekInt()
	{
		return toWeekInt;
	}
	
	public String[] getCourseNameString()
	{
		return courseNameString;
	}
	
	public String[] getPlaceString()
	{
		return placeString;
	}
	
	DocumentBuilderFactory documentBuilderFactory  = DocumentBuilderFactory.newInstance();
	
	public void readXml(int week, int dayOfWeek,File fileMain,File fileAdd) throws Exception
	{
		
		//reset all variable
		for(int i = 0; i < courseNameString.length; i++)
		{
			courseNameString[i] = "";
			fromWeekInt[i] = 0;
			toWeekInt[i] = 0;
			id[i] = 0;
			placeString[i] = "";
		}
		
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		mainFileDocument = documentBuilder.parse(fileMain);
		Element dayListElement = mainFileDocument.getDocumentElement();
		NodeList dayOfWeekNodeList = dayListElement.getElementsByTagName("day");
		Node dayOfWeekNode = dayOfWeekNodeList.item((dayOfWeek-1));
		NodeList courseList = dayOfWeekNode.getChildNodes();
		for(int j=0;j<courseList.getLength();j++)
		{
			int m = 0;	
			Node courseNode = courseList.item(j);
			if(courseNode.hasAttributes()&&(!courseNode.getAttributes().getNamedItem("fromWeek").getTextContent().equals("")))
			{
				if(courseNode.getTextContent()!=null)
				{ 
					Node fromWeekAttr = courseNode.getAttributes().getNamedItem("fromWeek");
					Node toWeekAttr = courseNode.getAttributes().getNamedItem("toWeek");
					Node placeAttr = courseNode.getAttributes().getNamedItem("place");
					Node idAttr = courseNode.getAttributes().getNamedItem("id");
					
					int fromWeekInt_2 = Integer.parseInt(fromWeekAttr.getTextContent());
					int toWeekInt_2 = Integer.parseInt(toWeekAttr.getTextContent());
					if(week>=fromWeekInt_2&&week<=toWeekInt_2)
					{
						id[m] = Integer.parseInt(idAttr.getTextContent());
						fromWeekInt[id[m]-1] = fromWeekInt_2;
						toWeekInt[id[m]-1] = toWeekInt_2;
						courseNameString[id[m]-1] = courseNode.getTextContent();
						placeString[id[m]-1] = placeAttr.getTextContent();
					}
				}
				m++;
			}
		}
		
		//如果附表为null，则不进行读取操作
		if (fileAdd == null) {
			return;
		}
		
		addFileDocument = documentBuilder.parse(fileAdd);
		Element itemElement = addFileDocument.getDocumentElement();
		NodeList itemList = itemElement.getElementsByTagName("item");
		
		for(int i = 0; i<itemList.getLength(); i++)
		{
			Node itemNode = itemList.item(i);
			if(itemNode.hasAttributes())
			{
				
				int fromWeek = Integer.parseInt(itemNode.getAttributes().getNamedItem("fromWeek").getTextContent());
				int toWeek = Integer.parseInt(itemNode.getAttributes().getNamedItem("toWeek").getTextContent());
				int dayOfWeek_temp = Integer.parseInt(itemNode.getAttributes().getNamedItem("dayOfWeek").getTextContent());
				
				if(fromWeek <= week && week <= toWeek && dayOfWeek_temp == dayOfWeek )
				{
					int turn = Integer.parseInt(itemNode.getAttributes().getNamedItem("turn").getTextContent());
					placeString[turn-1] = itemNode.getAttributes().getNamedItem("place").getTextContent();
					courseNameString[turn-1] = itemNode.getTextContent();
				}
			}
			
		}
	}
	
	public static boolean createAddFile()
	{
		return false;
	}
	
}
